package com.bbx.taxi.client.pay.line;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Message;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.PayBuild;
import com.bbx.api.sdk.model.passanger.Return.pay.Pay;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.pay.PayNet;
import com.bbx.api.util.LogUtils;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Task.BaseAsyncTask;

/** 
 * @Path com.bbx.taxi.client.pay.line.PayLine.java
 * @Author 严旭文
 * @Date 2016-7-27 上午10:08:43
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class PayLine {

	public Activity mContext;

	public String order_id;

	public String driver_id;
	// 支付完成接口
	public interface OnLinePayFinishListener {
		public void onLinePayFinish(boolean isSuccess);
	}

	public static OnLinePayFinishListener mOnLinePayFinishListener = null;

	public static void setOnLinePayFinishListener(OnLinePayFinishListener l) {
		mOnLinePayFinishListener = l;
	}
	public PayLine(Activity context, String order_id, String driver_id) {
		this.mContext = context;
		this.order_id = order_id;
		this.driver_id = driver_id;
	}
	public void pay(int cashAmt) {
		MyApplication myApplication = MyApplication.getInstance();
		// 访问服务器获取支付订单
		PayBuild mPayBuild = new PayBuild(mContext);
		mPayBuild.uid = myApplication.getUid();
		mPayBuild.access_token = myApplication.getToken();
		mPayBuild.order_id = order_id;
		mPayBuild.orderType = "2";
		mPayBuild.gateway = "line";
		mPayBuild.cashAmt = cashAmt+"";
		mPayBuild.driver_id = driver_id;
		LinepayTask mLinepayTask = new LinepayTask(mContext, mPayBuild);
		mLinepayTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	// 访问服务器获取支付订单
	// //////////////////////////////////////////////////
	public class LinepayTask extends BaseAsyncTask {
		PayBuild mPayBuild;

		public LinepayTask(Activity context, PayBuild mPayBuild) {
			super(context);
			this.mPayBuild = mPayBuild;
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mPayBuild).getJson();
			return new PayNet(context, json);
		}

		@Override
		public void onSuccess(int status,Object object) {
			if (object != null) {
				Pay mPay = (Pay) object;
				Pay.Detail detail = mPay.getDetail();
				if (detail != null && detail.geteWay() != null
						&& detail.geteWay().equals("line")) {
					if (mOnLinePayFinishListener != null)
						mOnLinePayFinishListener.onLinePayFinish(true);
				}
			} else {
				ToastUtil.showToast(context, R.string.no_network);
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			ToastUtil.showToast(context, R.string.no_network);
		}
	}
}
